package com.example.study.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.study.common.api.ResultUtil;
import com.example.study.mapper.TeacherMapper;
import com.example.study.model.StudentPO;
import com.example.study.model.TeacherPO;
import com.example.study.model.TeacherVO;
import com.example.study.service.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, TeacherPO> implements TeacherService {

    @Resource
    private TeacherMapper teacherMapper;

    @Override
    public ResultUtil saveTeacher(TeacherVO teacherVO) {
        log.info("save teacher begin, params: {}", teacherVO.toString());
        //把TeacherBO转换成TeacherPO
        TeacherPO teacherPO = new TeacherPO();
        BeanUtils.copyProperties(teacherVO, teacherPO);
        log.info("save teacher copy teacherVO to teacherPO, params: {}", teacherPO.toString());
        int row = teacherMapper.insert(teacherPO);
        if (row > 0){
            log.info("save teacher success. teacher.id: {}", teacherPO.getId());
            return ResultUtil.success(teacherPO);
        }
        log.info("save teacher fail.");
        return ResultUtil.failed("保存教室信息失败");
    }

    @Override
    public ResultUtil getTeacherPage(int pageIndex, int pageSize, TeacherVO teacherVO) {
        log.info("get teacher page begin, pageIndex: {}, pageSize: {}, search params: {}", pageIndex, pageSize, teacherVO.toString());
        //把TeacherBO转换成TeacherPO
        TeacherPO teacherPO = new TeacherPO();
        BeanUtils.copyProperties(teacherVO, teacherPO);
        log.info("get teacher copy teacherVO to teacherPO, params: {}", teacherPO.toString());

        Page<TeacherPO> page = new Page<>(pageIndex, pageSize);
        QueryWrapper<TeacherPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(teacherPO);
        queryWrapper.orderByDesc("id");
        IPage<TeacherPO> iPage = teacherMapper.selectPage(page, queryWrapper);

        log.info("get teacher page end, search result: {}", iPage.getRecords());
        return ResultUtil.success(iPage);
    }

    @Override
    public ResultUtil updateTeacher(TeacherVO teacherVO) {
        log.info("update teacher begin, update params: {}", teacherVO.toString());
        //把TeacherBO转换成TeacherPO
        TeacherPO teacherPO = new TeacherPO();
        BeanUtils.copyProperties(teacherVO, teacherPO);
        log.info("update teacher copy teacherVO to teacherPO, params: {}", teacherPO.toString());

        int row = teacherMapper.updateById(teacherPO);
        if (row > 0){
            log.info("update teacher success end, update after params: {}", teacherPO);
            return ResultUtil.success(true);
        }
        return ResultUtil.failed("修改老师信息失败");
    }

    @Override
    public ResultUtil deleteTeacher(Long id) {
        log.info("delete teacher begin, delete id: {}", id);
        int row = teacherMapper.deleteById(id);
        if (row > 0){
            log.info("delete teacher success");
            return ResultUtil.success(true);
        }
        return ResultUtil.failed("删除老师失败");
    }
}
